package com.romotc.json.tools;

import java.io.IOException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import org.codehaus.jackson.JsonGenerator;  
import org.codehaus.jackson.JsonProcessingException;  
import org.codehaus.jackson.map.JsonSerializer;  
import org.codehaus.jackson.map.SerializerProvider;  
  
/** 
 *  java日期对象经过Jackson库转换成JSON日期格式化自定义类 
 * @author godfox 
 * @date 2010-5-3 
 */  
public class CustomDateSerializer extends JsonSerializer<Date> {  
		private String dateFormat = "yyyy-MM-dd-HH-mm-ss";
		public CustomDateSerializer() {
			
		}
		
		/**
		 * 自定义日期格式
		 * @param dateFormat
		 */
		public CustomDateSerializer(String dateFormat) {
			this.dateFormat = dateFormat;
		}
		//2012-03-29-20-49-37
        @Override  
        public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {  
                SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);  
                String formattedDate = formatter.format(value);  
                jgen.writeString(formattedDate);  
        }  
}  